package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.utils.PreferenceHelper;

import java.util.List;

/**
 * Created by zbh on 15/11/30.
 */
public class GroupDetailsAdapter extends RecyclerView.Adapter<GroupDetailsAdapter.GroupDetailsViewHolder> {
    private List<String> list;
    private EMGroup group;
    private Context context;

    public GroupDetailsAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<String> list, EMGroup group) {
        this.list = list;
        this.group = group;
        notifyDataSetChanged();
    }

    public boolean isLength() {
        return list != null && list.size() > 0;
    }

    @Override
    public GroupDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_groupdetail_item, null);
        return new GroupDetailsViewHolder(view);
    }


    @Override
    public void onBindViewHolder(GroupDetailsViewHolder holder, int position) {
        if (isLength()) {
            if (position < list.size()) {
                if (position == 0){
                    holder.image_huang.setVisibility(View.VISIBLE);
                    String avatar = PreferenceHelper.getInstance().getThreeCurrentUserAvatar();
                    if (avatar != null){
                    Glide.with(context).load(avatar).placeholder(R.drawable.dl_default_avatar)
                            .error(R.drawable.dl_default_avatar).into(holder.image_header);
                    }else {
                        holder.image_header.setImageResource(R.drawable.dl_default_avatar);
                    }
                }
                String userName = list.get(position);
                holder.user_name.setText(userName);
                UserContact userContact = DatabaseHelper.getInstance().queryContactPeopleBynickName(userName);
                if (userContact != null){
                    String avater = userContact.getAvatar();
                    if (avater != null){
                        Glide.with(context).load(avater).placeholder(R.drawable.dl_default_avatar)
                                .error(R.drawable.dl_default_avatar).into(holder.image_header);
                    }
                }else {
                    holder.image_header.setImageResource(R.drawable.dl_default_avatar);
                }
            }


            if (position == list.size()&& group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
                holder.image_header.setImageResource(R.mipmap.dl_iconfont_jiahao1);
                holder.user_name.setVisibility(View.INVISIBLE);
            }else if (position == list.size()){
                holder.image_header.setVisibility(View.GONE);
                holder.user_name.setVisibility(View.GONE);
            }


            if (position == list.size() + 1 && group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
                holder.image_header.setImageResource(R.mipmap.dl_iconfont_jianhao2);
                holder.user_name.setVisibility(View.INVISIBLE);
            }else if (position == list.size() + 1){
                holder.image_header.setVisibility(View.GONE);
                holder.user_name.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public int getItemCount() {
        return isLength() ? list.size() + 2 : 0;
    }

    class GroupDetailsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private ImageView image_header;
        private TextView user_name;
        private LinearLayout groupMember;
        private ImageView image_huang;

        public GroupDetailsViewHolder(View itemView) {
            super(itemView);
            image_header = (ImageView) itemView.findViewById(R.id.image_header);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            groupMember = (LinearLayout) itemView.findViewById(R.id.groupMember);
            image_huang = (ImageView) itemView.findViewById(R.id.image_huan);
            groupMember.setOnClickListener(this);
            groupMember.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.groupMember:
                    if (listener != null && group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
                        listener.addMember( list,getLayoutPosition());
                        listener.removeMember(list,getLayoutPosition());
                    }

                    break;
            }
        }

        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
                case R.id.groupMember:
                    if (listener!= null && group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())){
                        listener.removeToblack(list,getLayoutPosition());
                    }
                    break;
            }
            return true;
        }
    }

    public interface OnGroupMemberItemClickListener {
        void removeToblack(List<String> list,int position);

        void addMember( List<String> list,int position);

        void removeMember(List<String> list,int position);
    }

    private OnGroupMemberItemClickListener listener;

    public void setOnGroupMemberItemClickListener(OnGroupMemberItemClickListener listener) {
        this.listener = listener;

    }
}

